package com.bugreport.web;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bugreport.configuration.BugTrackerConfigurationLoader;
import com.bugreport.model.Issue;
import com.bugreport.utils.BugReportException;

public class ReportIssueController extends AbstractController {

	private static final Logger logger = Logger.getLogger(ReportIssueController.class);

	public final static String SUCCESS_VIEW = "issueSuccessfulReport";
	public final static String SUCCESS_ISSUE_ID = "issueId";

	public final static String PARAM_ISSUE_TITLE = "issueTitle";
	public final static String PARAM_ISSUE_DESCRIPTION = "issueDescription";
	public final static String PARAM_ISSUE_PAGE_URL = "pageUrl";
	public final static String PARAM_ISSUE_PAGE_PAGE_HTML_CONTENT = "pageHtmlContent";

	private BugTrackerConfigurationLoader configurationLoader;

	public void setConfigurationLoader(BugTrackerConfigurationLoader configLoader) {
		this.configurationLoader = configLoader;
	}

	public String reportIssue(Issue issue) throws BugReportException {

		if (issue == null) {
			throw new BugReportException(400);
		} else if (issue.getTitle() == null || issue.getTitle().trim().length() == 0) {
			throw new BugReportException(401);
		} else if (issue.getDescription() == null || issue.getDescription().trim().length() == 0) {
			throw new BugReportException(402);
		} else if (issue.getPageUrl() == null || issue.getPageUrl().trim().length() == 0) {
			throw new BugReportException(403);
		} else if (issue.getPageHtmlContent() == null || issue.getPageHtmlContent().trim().length() == 0) {
			throw new BugReportException(404);
		}

		return configurationLoader.getMatchingConfiguration(issue.getPageUrl()).getConnector().reportIssue(issue);
	}

	@SuppressWarnings("rawtypes")
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		if (METHOD_GET.equals(request.getMethod())) {
			throw new BugReportException(405);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("ReportIssueController received attributes...");
			for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) {
				String attName = (String) e.nextElement();
				logger.debug(attName + " = " + request.getAttribute(attName));
			}

			logger.debug("");
			logger.debug("ReportIssueController received parameters...");
			for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
				String paramName = (String) e.nextElement();
				logger.debug(paramName + " = " + request.getParameter(paramName));
			}
		}

		// retrieving issue from parameters
		Issue issue = new Issue();
		issue.setTitle(request.getParameter(PARAM_ISSUE_TITLE));
		issue.setDescription(request.getParameter(PARAM_ISSUE_DESCRIPTION));
		issue.setPageUrl(request.getParameter(PARAM_ISSUE_PAGE_URL));
		issue.setPageHtmlContent(request.getParameter(PARAM_ISSUE_PAGE_PAGE_HTML_CONTENT));

		// reporting received issue
		String issueId = reportIssue(issue);

		// redirecting to issue successful report
		ModelAndView mav = new ModelAndView(SUCCESS_VIEW);
		mav.addObject(SUCCESS_ISSUE_ID, issueId);
		return mav;
	}
}
